
import SelectAppList from '@/components/select-app-list';

export default {
    name: 'collection-data',
    components: {
        SelectAppList
    },
    data() {
        return {
            form: {
                date: '',
                userType: [],
                appName: []
            },
            dataTotal: {},
            rules: {},
            tableData: [],
            tableMaxHeight: 500,
        };
    },
    methods: {
        // 获取列表数据
        getTableData() {
            const { appName, userType } = this.form;
            const params = {
                appNames: appName,
                userTypes: userType,
            };
            this.tableData = [];
            this.$api.report.queryAppUserRepay(params).then(res => {
                const {status, data, error} = res;
                if ('1' === status && '00000000' === error && data) {
                    const list = data.appUserRepays || [];
                    this.tableData = list.map(item => {
                        if (item.date && typeof item.date === 'string')
                            item.date = item.date.split(' ')[0];
                        return item;
                    });
                    const { shouldRepayCountSum, overdueCountSum, overdueRateSum, payedCountSum, notPayedCountSum, notPayedRateSum, shouldRepayAmountSum, payedAmountSum, notPayedAmountSum, notPayedAmountRateSum } = data;
                    this.dataTotal = { shouldRepayCountSum, overdueCountSum, overdueRateSum, payedCountSum, notPayedCountSum, notPayedRateSum, shouldRepayAmountSum, payedAmountSum, notPayedAmountSum, notPayedAmountRateSum };
                }
                this.tableMaxHeight = document.body.clientHeight - 165;
            });
        },
        exportExcel() {
            const { appName, userType } = this.form;
            const params = {
                appNames: appName,
                userTypes: userType,
            };
            const user = this.$storage.get("user");
            params.language = this.$storage.getLocal('language');
            params.token = user.token;
            const {$apiConf} = this;
            const url = this.$urlUtil.fillParams(params, $apiConf.baseUrl + $apiConf.urlType.report.appUserRepayExport, ["page"]);
            this.$openNewBlank(url);
        },
        toPercent(point){
            if (!point) {
                return '0%';
            }
            let str = Number(point * 100).toFixed(2);
            str += "%";
            return str;
        },
        // 总计数据
        getSummaries(param) {
            const { columns } = param;
            const sums = [];
            const { shouldRepayCountSum, overdueCountSum, overdueRateSum, payedCountSum, notPayedCountSum, notPayedRateSum, shouldRepayAmountSum, payedAmountSum, notPayedAmountSum, notPayedAmountRateSum } = this.dataTotal;
            const formatCurrency = this.$options.filters.formatCurrency;
            columns.forEach((item,index)=> {
                if (index === 0) {
                    sums[index] = this.$t('common.totals');
                    return;
                }
                switch (item.property) {
                    case 'shouldRepayCount':
                        sums[index] = shouldRepayCountSum;
                        break;
                    case 'overdueCount':
                        sums[index] = overdueCountSum;
                        break;
                    case 'overdueRate':
                        sums[index] = overdueRateSum;
                        break;
                    case 'payedCount':
                        sums[index] = payedCountSum;
                        break;
                    case 'notPayedCount':
                        sums[index] = notPayedCountSum;
                        break;
                    case 'notPayedRate':
                        sums[index] = notPayedRateSum;
                        break;
                    case 'shouldRepayAmount':
                        sums[index] = formatCurrency(shouldRepayAmountSum);
                        break;
                    case 'payedAmount':
                        sums[index] = formatCurrency(payedAmountSum);
                        break;
                    case 'notPayedAmount':
                        sums[index] = formatCurrency(notPayedAmountSum);
                        break;
                    case 'notPayedAmountRate':
                        sums[index] = notPayedAmountRateSum;
                        break;
                    default:
                        break;
                }
            })
            return sums;
        }
    },
    created() {
        this.getTableData();
    },
};
